package com.qf.controller;

import com.alibaba.excel.EasyExcel;
import com.qf.config.MdUnitMeasureReadListener;
import com.qf.entity.WmsCkManage;
import com.qf.service.WmsDetailInfoService;
import com.qf.until.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/detail")
@CrossOrigin
public class WmsDetailInfoController {

    @Autowired
    private WmsDetailInfoService service;

    /**
     * 查找数据详细信息
     * @return
     */
    @GetMapping("/infoAll")
    public R FindInfoAll(){
        List<WmsCkManage> list = service.findInfoAll();
        return R.ok(list);

    }

    /**
     * 删除数据
     * @return
     */
    @GetMapping("/del")
     public R deleteInfo(int id){
        boolean isok = service.deleteInfo(id);
        return R.ok(isok);
     }
    /**
     * 上传Excel文件
     */
    @PostMapping("/upload/excel")
    public R uploadExcel(MultipartFile file) {
        try {
            // 重点,此处第三个参数需要传入Service对象
            EasyExcel.read(file.getInputStream( ), WmsCkManage.class, new MdUnitMeasureReadListener(service)).sheet( ).doRead( );
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok( );
    }

    /**
     * 下载Excel
     * @param resp
     * @throws IOException
     */
     @GetMapping("/download/excel")
    public void downloadExcel(HttpServletResponse resp) throws IOException {

       List<WmsCkManage> list = service.findAll(null);

         // 下面这个注释是前端使用方式1,即a标签发请求时采用
         resp.setContentType("application/vnd.ms-excel");
         resp.setCharacterEncoding("utf-8");
         // 这里URLEncoder.encode可以防止中文乱码
         String fileName = URLEncoder.encode("申请出库明细", "UTF-8");
         resp.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
         EasyExcel.write(resp.getOutputStream(), WmsCkManage.class).sheet("申请出库明细").doWrite(list);


    }

}
